package edu.thu.thss.yxy.gui.custom;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JPanel;

import edu.thu.thss.yxy.audionotifier.AlertManager;

/**
 * The DialPanel contains the dial buttons.
 */

public class DialPanel extends JPanel implements MouseListener{
	private Font 	buttonTextFont = new Font("Verdana", Font.BOLD, 12);
	
	private JButton oneButton	= new JButton("1");
	private JButton twoButton	= new JButton("2");
	private JButton threeButton	= new JButton("3");
	private JButton fourButton	= new JButton("4");
	private JButton fiveButton	= new JButton("5");
	private JButton sixButton	= new JButton("6");
	private JButton sevenButton	= new JButton("7");
	private JButton eightButton	= new JButton("8");
	private JButton nineButton	= new JButton("9");
	private JButton starButton	= new JButton("*");
	private JButton zeroButton	= new JButton("0+");
	private JButton diezButton	= new JButton("#");
	
	private JPanel dialPadPanel = new JPanel(new GridLayout(4, 3, 5, 5));
	
	public DialPanel(){
		super(new FlowLayout(FlowLayout.CENTER));
		
		this.init();
	}
	
	public void init(){
	
		oneButton.setFont(this.buttonTextFont);
		twoButton.setFont(this.buttonTextFont);
		threeButton.setFont(this.buttonTextFont);
		fourButton.setFont(this.buttonTextFont);
		fiveButton.setFont(this.buttonTextFont);
		sixButton.setFont(this.buttonTextFont);
		sevenButton.setFont(this.buttonTextFont);
		eightButton.setFont(this.buttonTextFont);
		nineButton.setFont(this.buttonTextFont);
		zeroButton.setFont(this.buttonTextFont);
		diezButton.setFont(this.buttonTextFont);
		starButton.setFont(this.buttonTextFont);
		
		oneButton.setName("one");
		twoButton.setName("two");
		threeButton.setName("three");
		fourButton.setName("four");
		fiveButton.setName("five");
		sixButton.setName("six");
		sevenButton.setName("seven");
		eightButton.setName("eight");
		nineButton.setName("nine");
		zeroButton.setName("zero");
		diezButton.setName("diez");
		starButton.setName("star");

		oneButton.addMouseListener(this);
		twoButton.addMouseListener(this);
		threeButton.addMouseListener(this);
		fourButton.addMouseListener(this);
		fiveButton.addMouseListener(this);
		sixButton.addMouseListener(this);
		sevenButton.addMouseListener(this);
		eightButton.addMouseListener(this);
		nineButton.addMouseListener(this);
		zeroButton.addMouseListener(this);
		diezButton.addMouseListener(this);
		starButton.addMouseListener(this);
		
		dialPadPanel.add(oneButton);
		dialPadPanel.add(twoButton);
		dialPadPanel.add(threeButton);
		dialPadPanel.add(fourButton);
		dialPadPanel.add(fiveButton);
		dialPadPanel.add(sixButton);
		dialPadPanel.add(sevenButton);
		dialPadPanel.add(eightButton);
		dialPadPanel.add(nineButton);
		dialPadPanel.add(starButton);
		dialPadPanel.add(zeroButton);
		dialPadPanel.add(diezButton);
		
		this.add(dialPadPanel, BorderLayout.CENTER);
	}

	public void mouseClicked(MouseEvent e) {
	}

	public void mouseEntered(MouseEvent e) {
	}

	public void mouseExited(MouseEvent e) {
	}

	/**
     * Handles the <tt>MouseEvent</tt> triggered when user presses one of the
     * dial buttons.
     */
	public void mousePressed(MouseEvent e) {
		JButton button = (JButton) e.getSource();
        String buttonName = button.getName();

        String DTMFTone = "";
        
        if (buttonName.equals("one"))
        {
        	DTMFTone = AlertManager.DIAL_ONE;
        }
        else if (buttonName.equals("two"))
        {
        	DTMFTone = AlertManager.DIAL_TWO;
        }
        else if (buttonName.equals("three"))
        {
        	DTMFTone = AlertManager.DIAL_THREE;
        }
        else if (buttonName.equals("four"))
        {
        	DTMFTone = AlertManager.DIAL_FOUR;
        }
        else if (buttonName.equals("five"))
        {
        	DTMFTone = AlertManager.DIAL_FIVE;
        }
        else if (buttonName.equals("six"))
        {
        	DTMFTone = AlertManager.DIAL_SIX;
        }
        else if (buttonName.equals("seven"))
        {
        	DTMFTone = AlertManager.DIAL_SEVEN;
        }
        else if (buttonName.equals("eight"))
        {
        	DTMFTone = AlertManager.DIAL_EIGHT;
        }
        else if (buttonName.equals("nine"))
        {
        	DTMFTone = AlertManager.DIAL_NINE;
        }
        else if (buttonName.equals("zero"))
        {
        	DTMFTone = AlertManager.DIAL_ZERO;
        }
        else if (buttonName.equals("diez"))
        {
        	DTMFTone = AlertManager.DIAL_DIEZ;
        }
        else if (buttonName.equals("star"))
        {
        	DTMFTone = AlertManager.DIAL_STAR;
        }
        
        AlertManager.getAlertClip(DTMFTone, true).play();
	}

	public void mouseReleased(MouseEvent e) {
	}
}
